package com.lwisn.demo.presentation;

import java.awt.Color;
import java.awt.Font;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lwisn.demo.encoding.EncodingType;
import com.lwisn.demo.presentation.listener.GuiListener;

public class DemoViewer {

	private final Logger logger = LoggerFactory.getLogger(DemoViewer.class);
	
	private JFrame frame;
	private JPanel panel;
	private JTextArea textArea;
	private JScrollPane inputTextField;
	private JComboBox encryptionComboBox;
	private JButton button;
	private JButton inspectButton;
	private JLabel resultLabel;
	private GuiListener guiListener;
	
	private static int COMBOBOX_OFFSET_Y = 90;
	private static int COMBOBOX_OFFSET_X = 500;
	private static int TEXTFIELD_OFFSET_Y = 50;
	private static int TEXTFIELD_OFFSET_X = 50;
	private static int BUTTON_OFFSET_Y = 50;
	private static int BUTTON_OFFSET_X = 500;
	
	public DemoViewer() {
		
		guiListener = new GuiListener();
		
		initializeFrame();
		initializePanel();
		initializeChildren();
		
	}
	
	public static void main(String args[]) {
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				
				@Override
				public void run() {
					new DemoViewer();
				}
			});
		} catch (InvocationTargetException e) {
			
		} catch (InterruptedException e) {
			
		}
		
	}
	
	private void initializeFrame() {
		logger.debug("Initializing frame...");
		frame = new JFrame("MavenSpringDemo");
		frame.setSize(640, 460);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	private void initializePanel() {
		logger.info("Initializing panel...");
		panel = new JPanel();
		
		Color lightBlue = new Color(51, 153, 255);
		
		panel.setBackground(lightBlue);
		panel.setLayout(null);
		
		frame.setContentPane(panel);
	}
	
	private void initializeChildren() {
		logger.info("Adding JButton...");
		
		button = new JButton();
		button.setText("Submit");
		button.setActionCommand("submit");
		
		button.setBounds(BUTTON_OFFSET_X, BUTTON_OFFSET_Y, 100, 30);
		button.addActionListener(guiListener);
		panel.add(button);
		
		logger.info("Adding JComboBox...");
		
		encryptionComboBox = new JComboBox<EncodingType>(new EncodingType[] 
				{EncodingType.UTF8, EncodingType.UNICODE});
		encryptionComboBox.setSelectedIndex(0);
		encryptionComboBox.setBounds(COMBOBOX_OFFSET_X, COMBOBOX_OFFSET_Y, 100, 30);
		encryptionComboBox.addActionListener(guiListener);
		panel.add(encryptionComboBox);
		
		logger.info("Adding JTextArea...");
		
		textArea = new JTextArea("Test trololo");
		textArea.setFont(new Font("Sarif", Font.PLAIN, 12));
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		
		logger.info("Adding JScrollPane...");
		
		inputTextField = new JScrollPane(textArea);
		inputTextField = new JScrollPane(textArea);
		inputTextField.setVerticalScrollBarPolicy(
		                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		inputTextField.setBounds(TEXTFIELD_OFFSET_X, TEXTFIELD_OFFSET_Y, 300, 300);
		panel.add(inputTextField);
		
		inspectButton = new JButton();
		inspectButton.setText("Inspect");
		inspectButton.setActionCommand("inspect");
		
		inspectButton.setBounds(BUTTON_OFFSET_X, BUTTON_OFFSET_Y + 100, 100, 30);
		inspectButton.addActionListener(guiListener);
		panel.add(inspectButton);
		
		guiListener.setTextArea(textArea);
		guiListener.setComboBox(encryptionComboBox);
	}
	
	

}
